package JUC.死锁;

/**
 * @author CKC
 * @DateTime 2021/6/3 11:15
 *
 * 带改正
 */
public class 活锁 {
    public static void main(String[] args) {
        MyDate myDate = new MyDate();
        new Thread(myDate::add,"t1---").start();
        new Thread(myDate::jianJian,"t2----").start();
        
        
    }
    
    
}

class MyDate {
    private volatile int num = 200000;
    
    public void add() {
        while (true) {
            num++;
            System.out.println(Thread.currentThread().getName() + "  " + num);
            stop();
        }
    }
    
    public void jianJian() {
        while (true) {
            num--;
            System.out.println(Thread.currentThread().getName() + "  " + num);
            stop();
        }
    }
    
    public void stop() {
        if (num < 0) {
            Thread.currentThread().stop();
        }
    }
    
}